package cn.lijiancn01.demo.system.service;

import cn.lijiancn01.demo.framework.config.mybatis.BaseMapperX;
import cn.lijiancn01.demo.framework.exception.BizException;
import cn.lijiancn01.demo.framework.model.PageResult;
import cn.lijiancn01.demo.framework.model.R;
import cn.lijiancn01.demo.framework.util.BeanUtil;
import cn.lijiancn01.demo.system.entity.Template;
import cn.lijiancn01.demo.system.entity.TemplateGroup;
import cn.lijiancn01.demo.system.mapper.TemplateGroupMapper;
import cn.lijiancn01.demo.system.vo.QueryTemplateGroupVO;
import cn.lijiancn01.demo.system.vo.SaveTemplateGroupVO;
import cn.lijiancn01.demo.system.vo.TemplateGroupVO;
import cn.lijiancn01.demo.system.vo.TemplateVO;
import com.google.common.collect.Lists;
import com.mybatisflex.core.query.If;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.service.IService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static cn.lijiancn01.demo.system.entity.table.TemplateGroupTableDef.TEMPLATE_GROUP;

@Service
public class TemplateGroupService implements IService<TemplateGroup> {
    @Autowired
    private TemplateGroupMapper templateGroupMapper;
    
    @Autowired
    private TemplateService templateService;

    @Override
    public BaseMapperX<TemplateGroup> getMapper() {
        return templateGroupMapper;
    }

    public TemplateGroupVO findByCode(String code) {
        return BeanUtil.copyBean(getMapper().selectOneByCode(code), TemplateGroupVO.class);
    }

    public R add(SaveTemplateGroupVO saveTemplateGroupVO) {
        TemplateGroupVO templateGroupVO = this.findByCode(saveTemplateGroupVO.getCode());
        if (templateGroupVO != null) {
            throw new BizException("编号重复");
        }
        TemplateGroup addBean = BeanUtil.copyBean(saveTemplateGroupVO, TemplateGroup.class);
        return R.data(getMapper().insertSelective(addBean));
    }

    public PageResult<TemplateGroupVO> findByCondition(QueryTemplateGroupVO queryVO) {
        QueryWrapper query = QueryWrapper.create()
                .from(TEMPLATE_GROUP)
                .where(TEMPLATE_GROUP.CODE.like(queryVO.getCode(), If::hasText))
                .orderBy(TEMPLATE_GROUP.ID, false);
        return getMapper().findByCondition(queryVO.getPage(), queryVO.getPerPage(), query, TemplateGroupVO.class);
    }

    public R remove(Long id) {
        TemplateGroup bean = getMapper().selectOneById(id);
        if (bean == null) {
            throw new BizException("操作的数据不存在");
        }
        List<TemplateVO> templateVOS = templateService.findByGroupCode(bean.getCode());
        if (CollectionUtils.isNotEmpty(templateVOS)) {
            throw new BizException("请先删除模板组下所有有效的模板再删除模板组");
        }
        return R.data(getMapper().deleteById(id));
    }

    @Transactional(rollbackFor = Exception.class)
    public R copy(Long id, SaveTemplateGroupVO saveTemplateGroupVO) {
        //被复制的模板组必须存在
        TemplateGroup bean = getMapper().selectOneById(id);
        if (bean == null) {
            throw new BizException("操作的数据不存在");
        }
        String newCode = saveTemplateGroupVO.getCode();
        //新模板组编号不能和原有的编号重复
        TemplateGroup TemplateGroup = getMapper().selectOneByCode(newCode);
        if (TemplateGroup != null) {
            throw new BizException(String.format("新编号%s已存在", newCode));
        }

        //新增模板组
        TemplateGroup newBean = new TemplateGroup();
        newBean.setCode(newCode);
        getMapper().insertSelective(newBean);
        //复制原组的模板到新组中
        List<TemplateVO> templateVOS = templateService.findByGroupCode(bean.getCode());
        if (CollectionUtils.isNotEmpty(templateVOS)) {
            List<Template> newTemplates = Lists.newArrayList();
            for (TemplateVO templateVO : templateVOS) {
                Template templateDO = BeanUtil.copyBean(templateVO, Template.class);
                templateDO.setGroupCode(newCode);
                templateDO.setCode(templateVO.getCode());
                templateDO.setId(null);
                newTemplates.add(templateDO);
            }
            templateService.insertBatch(newTemplates);
        }
        return R.data(newCode);
    }
}
